// pages/tenants/tenants.js
const app = getApp()
import Api from '../../utils/api';
import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    idCardImg: "",
    idCardImgS: '/img/add.png',
    idCardImgOneS: "/img/add.png",
    idCardImgOne: "",
    region: ['省份', '城市', '区县'],
    distributorTypeArr: ["优选配送员", "绑定配送员"], //配送员类型
    distributorTypeIndex: 0, //配送员类型索引
    businessmenName: "惠生活商户",//默认商户名称  页面不显示 接口需要的
    businessNumber: "20190819", //默认营业经营号 页面不显示 接口需要的
    businessType: '私营', //默认经营类型 页面不显示 接口需要的
    name: '',
    phone: '',
    address: '',
    password: '',
    passwordComfig: '',
    card: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    app.globalData.otherPageHideFlag = true;
  },
  bindRegionChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  //商户联系人
  nameClick(e){
    let _this  = this;
    _this.setData({
      name: e.detail.value
    })
  },
  //联系电话
  phoneClick(e){
    let _this = this;
    _this.setData({
      phone: e.detail.value
    })
  },
  //详细地址
  addressClick(e){
    let _this = this;
    _this.setData({
      address: e.detail.value
    })
  },
  //设置密码
  passwordClick(e){
    let _this = this;
    _this.setData({
      password: e.detail.value
    })
  },

  //确认密码
  passwordComfigClick(e){
    let _this = this;
    _this.setData({
      passwordComfig: e.detail.value
    })
  },
  //身份证
  cardClick(e){
    let _this = this;
    _this.setData({
      card: e.detail.value
    })
  },
  //配送
  bindPickerChange(e){
    let _this = this;
    //console.log(e)
    _this.setData({
      distributorTypeIndex: e.detail.value
    })
  },
  //上传图片
  fileUpload(e){
    let _this  = this;
    let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(_this.data.phone)) {
      wx.showToast({
        title: '手机号码格式不对',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    if (_this.data.password.length < 6) {
      wx.showToast({
        title: '密码不少于6位',
        icon: 'none',
        duration: 1500,
      })
      return;
    }
    if (!(_this.data.password != "" && _this.data.password == _this.data.passwordComfig)){
      wx.showToast({
        title: '密码匹配失败',
        icon: 'none',
        duration: 1500,
      })
      return;
    }
    if (_this.data.name == "" || _this.data.phone == "" || _this.data.address == "" || _this.data.card == "" || _this.data.region[0] == "省份") {
      wx.showToast({
        title: '请将信息填写完整',
        icon: 'none',
        duration: 1500,
      })
      return;
    }
    let index = e.currentTarget.dataset.index;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function(res) {
        //console.log(res)
        _this.uploadFile(index, res.tempFilePaths[0])
      },
    })
    
  },

  //上传的接口
  uploadFile(index,img){
    let _this = this;
    console.log(img)
    wx.uploadFile({
      url: app.globalData.Prefi + '/public/fileUpload',
      name: 'file',
      filePath: img,
      formData: {
        "returnName": index == 1 ? 'p1' : 'p2',
        "phone": _this.data.phone
      },
      success: function (res) {
        console.log(res.statusCode);
        if (res.statusCode == 200) {
          var data = JSON.parse(res.data);
          console.log(data);
          if(index == 1){
            _this.setData({
              idCardImgS: img,
              idCardImg: data.data.fileUrl
            })
          }else{
            _this.setData({
              idCardImgOneS: img,
             idCardImgOne: data.data.fileUrl
            })
          }
        } else {
          wx.showToast({
            title: '上传文件失败',
            icon: 'none',
            duration: 1500,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      }
    })
  },

  //拿去详细地址获拿取百度api
  addressDetailClick(){
    let _this  = this;
    console.log(_this.data.address);
    if (_this.data.address == ""){
      return;
    }
    wx.request({
      url: `http://api.map.baidu.com/cloudgc/v1?ak=iOYBmDKHicYmzZigGIGnurrPf1YMKlEj&address=${_this.data.address}`,
      method: 'GET',
      success: function(res){
        console.log("百度api",res)
        if (res.data.result[0]){
          let result = res.data.result[0];
          let region = [result.address_components.province, result.address_components.city, result.address_components.district]
          _this.setData({
            lat: result.location.lat.toFixed(7),
            lng: result.location.lng.toFixed(7),
            region: region
          })
        }else{
          wx.showToast({
            title: '请输入正确地址',
            icon: 'none',
            duration: 1500,
          })
          _this.setData({
            address: '',
          })
        }
        
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        console.log(res)
      },
    })
  },

  //提交
  submitClick(){
    let _this = this;
    if (_this.data.idCardImg == "/img/add.png" || _this.data.idCardImgOne == "/img/add.png"){
      wx.showToast({
        title: '添加身份照片',
        icon: 'none',
        duration: 1500,
      })
      return;
    }
    let data = {
      bigCustomerName: _this.data.businessmenName, //默认商户名称  页面不显示 接口需要的
      contactName: _this.data.name, //商户联系人
      phonenumber: _this.data.phone, //联系电话
      city: _this.data.region[1], //城市
      customerAddressName: _this.data.address,  //商户地址名称
      password: _this.data.password, //密码
      idCardNumber: _this.data.card, //身份证号
      lat: _this.data.lat, //营业地址经度 lngOne
      lng: _this.data.lng, //营业地址纬度 latOne
      businessType: _this.data.businessType, //默认经营类型
      licenseNumber: _this.data.businessNumber, //默认营业经营号 页面不显示 接口需要的
      bindingType: _this.data.distributorTypeIndex == 1 ? "BINDING" : "PREFERRED", //配送员类型绑定类型
      idCardFront: _this.data.idCardImg,
      idCardBack: _this.data.idCardImgOne,
      pricePerDay: 123, //日单价
      numOrdersPerDay: 10, //日单量 
    }
    console.log(data);
    //return;
    wx.request({
      url: app.globalData.Prefi + '/public/openplatform/bigcustomer/applications/str',
      data: data,
      method: "POST",
      success: function (res) {
        console.log("success",res)
        wx.showToast({
          title: '入驻成功,等待审核',
          icon: 'none',
          duration: 2000,
        });
        setTimeout(function(){
          wx.navigateBack({
            delta: 1,
          })
        },2000)

      },
      fail: function (res) {
        console.log("fail",res);
      },
      complete: function (res) { },
    })

  },



})